import requests
from collections import deque

# 初始化参数
key = 'cqqkcu49mds3vp4u4v8om3qtmmfl4l63038r1httn5tun9tu155u39k4kpkbsj2k'
domainId = '3'
# 所有分类信息保存在all_categories中，后面遍历保存到mongodb中，或者得到分类信息时直接插入也行。
all_categories = []


# 定义请求接口api的方法
def getCategoryInfos(categoryIds):
    url = "https://api.keepa.com/category?key=" + key + "&domain=" + domainId + "&category=" + str(categoryIds) + "&parents=1"
    response = requests.get(url)
    getSubCategoryIds = []
    if response.status_code == 200:
        data = response.json()
        for item in data['categories']:
            ret = {}
            if data['categories'][item]['isBrowseNode']:
                ret['parent'] = data['categories'][item]['parent']
                ret['catId'] = data['categories'][item]['catId']
                ret['name'] = data['categories'][item]['name']
                ret['children'] = data['categories'][item]['children']
                getSubCategoryIds.append(ret)
    return getSubCategoryIds


# 循环请求接口得到给定父分类的子分类，由于接口限制一次只能传递10个分类id获取对应的子分类信息数据
def recursionSubCategoriesDeque(parent_ids):
    # 定义一个双向队列，每次从队列头部取10个id去请求接口，接口返回的子分类信息id统统从队列尾部插入到该双向队列中
    category_id_list = deque(parent_ids)

    while category_id_list:
        batch_ids = [category_id_list.popleft() for _ in range(min(10, len(category_id_list)))]
        parent_ids_str = ','.join(str(i) for i in batch_ids)
        print('当前category_id_list长度:', len(category_id_list))

        # 返回给定10个ids的所有分类信息retData，
        retData = getCategoryInfos(parent_ids_str)

        for item in retData:
            print(item)

            all_categories.append(item)
            if item['children']:
                category_id_list.extend(item['children'])


parent_ids = [0]
# 调用函数并传递初始分类 ID 列表
recursionSubCategoriesDeque(parent_ids)
